﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnCommon;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Threading.Tasks;
using OnLineLogFileComm;
using System.Diagnostics;

namespace OnLineLogFileClient
{
    public partial class OnLineClient : Form
    {

        public OnLineClient()
        {
            InitializeComponent();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (aafLog != null)
            {
                aafLog.Dispose();
            }
            if (callTiming != null)
            {
                callTiming.Dispose();
            }
        }
        AAFLogTranJobAsync aafLog;
        CallTimingTranJobAsync callTiming;

        string path1 = @"D:\salesystem\svn_consignment.5173.com\Log";
        string path3 = @"D:\salesystem\consignment.5173.com\Log";
        string path2 = @"D:\salesystem\svn_vjsadmin.5173.com\log";

        private void button1_Click(object sender, EventArgs e)
        {
            var rootPath = "";
            if (Directory.Exists(this.textBox1.Text.Trim()))
                rootPath = this.textBox1.Text.Trim();
            else if (Directory.Exists(path1))
                rootPath = path1;
            else if (Directory.Exists(path3))
                rootPath = path3;
            else if (Directory.Exists(path2))
                rootPath = path2;
            else
            {
                this.aafLog_NotifyMsgEvent("文件找不到。");
                return;
            }

            this.button1.Enabled = false;
            this.label1.Text = "已启动传输：2.6为服务器";
            this.textBox1.Text = rootPath;

            var up = new UpdateJob();
            up.Run();
            up.Dispose();

            aafLog = new AAFLogTranJobAsync();
            aafLog.RootPath = rootPath;
            aafLog.NotifyMsgEvent += aafLog_NotifyMsgEvent;
            aafLog.Run();

            LogServices.LogService.Instance.Write("AAFLogTranJobAsync is run");

            callTiming = new CallTimingTranJobAsync();
            callTiming.RootPath = Path.Combine(rootPath, "CallTiming");
            callTiming.NotifyMsgEvent += aafLog_NotifyMsgEvent;
            callTiming.Run();
            LogServices.LogService.Instance.Write("CallTimingTranJobAsync is run");
        }

        void OnLineClient_Load(object sender, EventArgs e)
        {
            if (GloblSett.HasAutoRun)
            {
                this.button1_Click(this.button1, new EventArgs());
            }
        }

        void aafLog_NotifyMsgEvent(string msg)
        {
            
            this.Invoke(new Action(() =>
            {
                if (GloblSett.HasLogInFile)
                {

                    LogServices.LogService.Instance.Write(msg);
                    //return;
                }

                if (this.richTextBox1.TextLength > 200000)
                {
                    this.richTextBox1.Clear();
                }
                this.richTextBox1.AppendText(msg + Environment.NewLine);
                this.richTextBox1.ScrollToCaret();
                this.richTextBox1.Focus();
            }));
        }

    }
}
